<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<c:import url="/includes/header.jsp"></c:import>
<c:import url="/includes/modal.jsp"></c:import>
<title>添加资源</title>
</head>
<body>
	<div class="container">

		<div class="row">
			<div class="col-md-4"></div>
			<div class="col-md-4">
				<form class="form-horizontal" method="post" id="form"
					action="${pageContext.request.contextPath}/admin/resource/save.do">
					<input type="hidden" name="id" value="0">
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">资源名称:</label>
						<div class="col-sm-9">
							<input class="form-control" type="text" id="name" name="name"
								placeholder="资源名称">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">资源地址:</label>
						<div class="col-sm-9">
							<input class="form-control" type="text" id="url" name="url"
								placeholder="资源地址">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label" for="content">上级资源:</label>
						<div class="col-sm-9">
							<input class="form-control" type="text" id="content"
								data-toggle="modal" name="content" placeholder="选择上级资源"
								readonly="readonly"> <input class="form-control"
								type="hidden" id="parentId" name="parentId" value="0">
						</div>
					</div>
					<div class="col-md-4"></div>
					<div class="col-md-4" style="margin-left: 50%;">
						<button type="submit"  class="btn btn-primary btn-sm">确定</button>
					</div>
					<div class="col-md-4"></div>
				</form>
			</div>
			<div class="col-md-4"></div>
		</div>

	</div>
</body>
</html>
<script>
	$(function() {
		$("#content")
				.click(
						function() {
							var url = "${pageContext.request.contextPath}/admin/resource/setMenu.do";
							url += '?t=' + Math.random(1000);
							$('#myModal').modal({
								remote : url
							})
						});
		$(".btn").click(function() {
			validateForm();
		});

		function validateForm() {
			var validator = $("#form").validate({
				onsubmit : true,// 是否在提交时验证  
				errorElement : "em",
				success : function(em) {
				},
				submitHandler : function() {
					ajaxSubmitForm();
				},//这是关键的语句，配置这个参数后表单不会自动提交，验证通过之后会去调用的方法
				rules : {
					name : {
						required : true
					}
				},
				messages : {
					name : {
						required : "资源名称必须填写"
					}
				}
			});
		}

		function ajaxSubmitForm() {
			var hrefURL = "${pageContext.request.contextPath}/admin/resource/index.do";
			$.ajax({
				url : $("#form").attr("action"),
				data : $("#form").serialize(),
				method : "post",
				dataType : "json",
				async : true,
				success : function(data) {
					if (data.code == "0") {
						bootbox.alert(data.data, function() {
							window.location.href = hrefURL;
						});
					} else {
						bootbox.alert(data.msg);
					}
				},
				error : function(data) {
					bootbox.alert(data.msg);
				}
			});
		}
	})
</script>